package com.mdms.commands.user;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.Hibernate;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import com.mdms.dto.UdtUsers;
import com.mdms.interfaces.orm.CrudDAO;
import com.mdms.jsf.managedbeans.user.UserMBean;
import com.mdms.logger.SimplyMDLogger;
import com.mdms.commands.BaseCommand;

import org.hibernate.criterion.Restrictions;

public class UserCommand extends BaseCommand {

	private final static SimplyMDLogger logger = new SimplyMDLogger(UserCommand.class);
	
	private static final long serialVersionUID = 1L;

	UdtUsers users = new  UdtUsers();

	private String login;
	
	public UserCommand(ClientContext clientContext){
		super(clientContext);
	}
	
	public void execute() throws Exception {
		logger.debug("execute", "START");
		CrudDAO crudDAO = getDaoFactory().getCrudDAO();
		crudDAO.setPersistentClass(UdtUsers.class);
		Transaction tx=crudDAO.beginTransaction();
		try{
		if (login != null) {
			List criterion = new ArrayList();
			criterion.add(Restrictions.eq("usr_login", login));
			List result = crudDAO.findByCriterions(criterion);
			if (result != null && result.size() > 0) {
				users = (UdtUsers)result.get(0);
				
			}else{
				this.errorCode=-1;
				this.errorDesc="MESSAGE FROM BUNDLE USER NOT FOUND";
			}
			tx.commit();
		}
		}catch(Exception e){
			this.errorCode=-1;
			this.errorDesc=e.getMessage();
			logger.error("execute", e);
		}
		
		logger.debug("execute", "END");
		
	}

	public UdtUsers getUsers() {
		return users;
	}

	public void setUsers(UdtUsers users) {
		this.users = users;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}



}
